package org.springframework.webflow.wanda;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("WandaUserService")
@Repository
public class JpaWandaUserService implements Serializable, WandaUserService {

    private static final long serialVersionUID = 1L;
    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
	this.em = em;
    }

    @Transactional()
    public void createWandaUser(WandaUser WandaUser) {
	// WandaUser = new WandaUser("sanyi", "sanyi", "sanyi");
	em.persist(WandaUser);
	Query q = em.createNativeQuery("select * from WandaUser where name = " + "\'" + WandaUser.getUsername() + "\'",
		WandaUser.class);
	// WandaUser u = (WandaUser) q.getSingleResult();
	// return u;
    }

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    public List<WandaUser> listWandaUsers() {
	Query q = em.createNativeQuery("select * from WandaUser", WandaUser.class);
	List<WandaUser> WandaUsers = q.getResultList();
	return WandaUsers;
    }

    @Transactional
    public void modifyWandaUserData(WandaUser WandaUserToModify) {
	em.merge(WandaUserToModify);
    }

}
